package cn.bgs.serviceedu.controller;


import cn.bgs.serviceedu.entity.Customer;
import cn.bgs.serviceedu.service.CustomerService;
import cn.bgs.serviceutils.utils.CSException;
import cn.bgs.serviceutils.utils.R;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 客户表 前端控制器
 * </p>
 *
 * @author YiChen
 * @since 2021-10-20
 */
@Api(description = "客户表")
@RestController
@RequestMapping("/serviceedu/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;



    @ApiOperation(value = "分页模糊查询")
    @PostMapping("/findAll")
    @CachePut(key = "'CustomerList'",value = "CustomerList")
    public R findAll(@RequestBody(required = false) Customer customer){
        LambdaQueryWrapper<Customer> wrapper = Wrappers.<Customer>lambdaQuery()
                .like(StringUtils.isNoneBlank(customer.getCustomerName()),Customer::getCustomerName,StringUtils.trimToEmpty(customer.getCustomerName()))
                .eq(Customer::getDeleteFlag,0)
                .orderByAsc(Customer::getCreateTime);
        return R.ok().data("list",customerService.list(wrapper));
    }


    /**
     * 根据ID删除
     * @param customerId
     * @return
     */
    @ApiOperation(value = "根据ID删除")
    @DeleteMapping("/del/{customerId}")
    @CachePut(key = "'CustomerList'",value = "CustomerList")
    public R del(@PathVariable Integer customerId){
        boolean b = false;
        try {
            b = customerService.removeById(customerId);
        } catch (Exception e) {
            throw new CSException(20001,"删除失败");
        }
        if (b){
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }


    /**
     * 根据ID单查
     * @param customerId
     * @return
     */
    @ApiOperation(value = "根据ID单查")
    @GetMapping("/findById/{customerId}")
    public R findById(@PathVariable Integer customerId){
        return   R.ok().data("list",customerService.getById(customerId));
    }


    /**
     * 添加用户
     * @param customer
     * @return
     */
    @ApiOperation(value = "注册客户")
    @PostMapping("/addCustomer")
    @CachePut(key = "'CustomerList'",value = "CustomerList")
    public R addUser(@RequestBody Customer customer ){
        customer.getPassWord();
        customer.getPayPwd();
        MD5 md5 = new MD5();
        String s = md5.digestHex(customer.getPassWord());
        customer.setPassWord(s);
        String s1 = md5.digestHex(customer.getPayPwd());
        customer.setPayPwd(s1);

        boolean save = false;
        try {
            save = customerService.save(customer);
        } catch (Exception e) {
            throw new CSException(20001,"注册失败");
        }
        if (save){
            return R.ok().message("注册成功");
        }
        return R.error().message("注册失败,请重试");
    }


    /**
     * 修改用户
     * @param customer
     * @return
     */
    @ApiOperation(value = "修改客户")
    @PutMapping("/update")
    @CachePut(key = "'CustomerList'",value = "CustomerList")
    public R update(@RequestBody Customer customer){
        boolean update = false;
        try {
            update = customerService.updateById(customer);
        } catch (Exception e) {
            throw new CSException(20001,"修改失败");
        }
        if (update){
            return  R.ok().message("修改成功");
        }else{
            return R.error().message("修改失败");
        }
    }



}
